package com.sdtv.mediunfusion.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @ProjectName: haikansport
 * @Package: com.haikan.sport.widget
 * @ClassName: CustomerDialog
 * @Description: 自定义dialog
 * @Author: 小郭同学
 * @CreateDate: 2019-05-16 09:48
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CustomerDialog extends Dialog {

    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;


    private CustomerDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    private CustomerDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    private Builder builder() {
        return new Builder();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(view);
        setCanceledOnTouchOutside(cancelTouchout);

        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = Gravity.CENTER;
        if (height == 0) {
            lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
        } else {
            lp.height = height;
        }
        if (width == 0) {
            lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
        } else {
            lp.width = width;
        }

        win.setAttributes(lp);
    }

    public static final class Builder {

        private Context context;
        private int height = 0;
        private int width = 0;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;


        public Builder with(Context context) {
            this.context = context;
            return this;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }


        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }


        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public Builder setText(int viewRes, String text) {

            View tempView = view.findViewById(viewRes);
            if (tempView instanceof TextView) {
                ((TextView) tempView).setText(text);
            }
            return this;
        }

        public Builder setText(int viewRes, int textRes) {

            View tempView = view.findViewById(viewRes);
            if (tempView instanceof TextView) {
                ((TextView) tempView).setText(textRes);
            }
            return this;
        }


        public Builder setTextColor(int viewRes, int textRes) {

            View tempView = view.findViewById(viewRes);
            if (tempView instanceof TextView) {
                ((TextView) tempView).setTextColor(textRes);
            }
            return this;
        }

        public Builder setVisibile(int viewRes, boolean isShow) {

            View tempView = view.findViewById(viewRes);
            if (null != tempView) {
                if (isShow) {
                    tempView.setVisibility(View.VISIBLE);
                } else {
                    tempView.setVisibility(View.GONE);
                }
            }
            return this;
        }


        public Builder setImageResource(int viewRes, int resId) {
            View tempView = view.findViewById(viewRes);
            if (tempView instanceof ImageView) {
                ((ImageView) tempView).setImageResource(resId);
            }
            return this;
        }

        public CustomerDialog build() {
            if (resStyle != -1) {
                return new CustomerDialog(this, resStyle);
            } else {
                return new CustomerDialog(this);
            }
        }
    }
}
